(***** SOLVING RLC CIRCUIT EQS*****)

(* Solving the circuit equation *)

FullSimplify[DSolve[{Rr*Q'[t] + L*Q''[t] +Q[t]/c==0, Q[0]== B, Q'[0]== 0},{Q[t]},t]]

(* Assigning name *)

QRLC[t_] :=B (E^(-((Rr t)/(2 L)))) (Cosh[(((Sqrt[-4 L+c (Rr^2)]) t)/(2 (Sqrt[c]) L))]+(((Sqrt[c]) Rr Sinh[(((Sqrt[-4 L+c (Rr^2)]) t)/(2 (Sqrt[c]) L))])/(Sqrt[-4 L+c (Rr^2)])))

(* time-derivating charge yields current *)

D[QRLC[t],t]

(* Assigning name *)

JRLC[t_] :=-((B (E^(-((Rr t)/(2 L)))) Rr (Cosh[(((Sqrt[-4 L+c (Rr^2)]) t)/(2 (Sqrt[c]) L))]+(((Sqrt[c]) Rr Sinh[(((Sqrt[-4 L+c (Rr^2)]) t)/(2 (Sqrt[c]) L))])/(Sqrt[-4 L+c (Rr^2)]))))/(2 L))+B (E^(-((Rr t)/(2 L)))) (((Rr Cosh[(((Sqrt[-4 L+c (Rr^2)]) t)/(2 (Sqrt[c]) L))])/(2 L))+(((Sqrt[-4 L+c (Rr^2)]) Sinh[(((Sqrt[-4 L+c (Rr^2)]) t)/(2 (Sqrt[c]) L))])/(2 (Sqrt[c]) L)))

(***** SOLVING RLC CIRCUIT WITH EXTRA BRANCH EQS *****)

(* Solving the circuit system of equations *)

FullSimplify[DSolve[{Q1[t]/c - (Q2'[t]-Q1'[t])*R==0, (Q2'[t]-Q1'[t])*R + Rr*Q2'[t] + L*Q2''[t]==0, Q1[0]==0, Q1'[0]==A, Q2[0]==0, Q2'[0]==A},{Q1[t],Q2[t]},t]]

(* Assigning names *)

Q1[t_]:=((A c (E^(-(((L+c R Rr+(Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])) t)/(2 c L R)))) (-1+(E^(((Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)]) t)/(c L R)))) L R)/(Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)]))
Q2[t_] :=(1/(2 (R+Rr) (Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])))A (E^(-(((L+c R Rr+(Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])) t)/(2 c L R)))) L (L-(E^(((Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)]) t)/(c L R))) L+c (-1+(E^(((Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)]) t)/(c L R)))) R (2 R+Rr)-(1+(E^(((Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)]) t)/(c L R)))-2 (E^(((L+c R Rr+(Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])) t)/(2 c L R)))) (Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)]))

(* Time-derivating the charge in each loop gives the respective current *)

D[Q1[t],t]
D[Q2[t],t]

(* Assigning names *)

J1[t_] := A (E^((((Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)]) t)/(c L R))-(((L+c R Rr+(Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])) t)/(2 c L R))))-((A (E^(-(((L+c R Rr+(Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])) t)/(2 c L R)))) (-1+(E^(((Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)]) t)/(c L R)))) (L+c R Rr+(Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])))/(2 (Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])))
J2[t_] := -(A (E^(-(((L+c R Rr+(Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])) t)/(2 c L R)))) (L+c R Rr+(Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])) (L-(E^(((Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)]) t)/(c L R))) L+c (-1+(E^(((Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)]) t)/(c L R)))) R (2 R+Rr)-(1+(E^(((Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)]) t)/(c L R)))-2 (E^(((L+c R Rr+(Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])) t)/(2 c L R)))) (Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])))/(4 c R (R+Rr) (Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)]))+(1/(2 (R+Rr) (Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])))A (E^(-(((L+c R Rr+(Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])) t)/(2 c L R)))) L (-(((E^(((Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)]) t)/(c L R))) (Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)]))/(c R))+(((E^(((Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)]) t)/(c L R))) (2 R+Rr) (Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)]))/L)-(Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)]) ((((E^(((Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)]) t)/(c L R))) (Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)]))/(c L R))-(((E^(((L+c R Rr+(Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])) t)/(2 c L R))) (L+c R Rr+(Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])))/(c L R))))

(***** NUMERICAL VALUES AND PLOTTING *****)

c=0.0288 ;(*capcity*)
R=0.0000001;(*diode resistance*)
Rr = 0.06; (*main rlc circuit resistance*)
L=0.000196666; (*inductor*)
B = 450*c; (*initial charge*)

(* STARTING CURRENT "A" FOR RLC -> RLC WITH C-DIODE TRANSITION *)

tswitch = Re[t/.FindRoot[QRLC[t], {t, 0.005}]]

A=JRLC[tswitch]

(* Plotting basic RLC voltage and current *)

Plot[{QRLC[t]}, {t,0,0.03}, PlotRange->All]
Plot[{JRLC[t]}, {t,0,0.03}, PlotRange->All]

(* Plotting the voltage across capacitor (Q1) and the current in inductor (J2) after the switching*)

Plot[{Q1[t]/c}, {t,0,0.03}, PlotRange->All]
Plot[{J2[t]}, {t,0,0.03}, PlotRange->All, PlotPoints -> 500]

(* Complete functions and plots *)

Qgen[t_] := Piecewise[{{QRLC[t], t<tswitch}, {Q1[t-tswitch],t>tswitch}}]
Jgen[t_] := Piecewise[{{JRLC[t], t<tswitch}, {J2[t-tswitch],t>tswitch}}]

Plot[{Qgen[t]/c}, {t,0,0.03}, PlotRange->All]
Plot[{Jgen[t]}, {t,0,0.03}, PlotRange->All]

Manipulate[Plot[{Piecewise[{{B (E^(-((Rr t)/(2 L)))) (Cosh[(((Sqrt[-4 L+c (Rr^2)]) t)/(2 (Sqrt[c]) L))]+(((Sqrt[c]) Rr Sinh[(((Sqrt[-4 L+c (Rr^2)]) t)/(2 (Sqrt[c]) L))])/(Sqrt[-4 L+c (Rr^2)]))), t<tswitch}, {(A c (E^(-(((L+c R Rr+(Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])) (t-tswitch))/(2 c L R)))) (-1+(E^(((Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)]) (t-tswitch))/(c L R)))) L R)/((Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])),t>tswitch}}]/c}, {t,0,0.03}, PlotRange->{-150,450}], {R, 0.0000001, 1, Appearance -> "Labeled"}, SaveDefinitions -> True]

Manipulate[Plot[{Piecewise[{{-((B (E^(-((Rr t)/(2 L)))) Rr (Cosh[(((Sqrt[-4 L+c (Rr^2)]) t)/(2 (Sqrt[c]) L))]+(((Sqrt[c]) Rr Sinh[(((Sqrt[-4 L+c (Rr^2)]) t)/(2 (Sqrt[c]) L))])/(Sqrt[-4 L+c (Rr^2)]))))/(2 L))+B (E^(-((Rr t)/(2 L)))) (((Rr Cosh[(((Sqrt[-4 L+c (Rr^2)]) t)/(2 (Sqrt[c]) L))])/(2 L))+(((Sqrt[-4 L+c (Rr^2)]) Sinh[(((Sqrt[-4 L+c (Rr^2)]) t)/(2 (Sqrt[c]) L))])/(2 (Sqrt[c]) L))), t<tswitch}, {-(A (E^(-(((L+c R Rr+(Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])) (t-tswitch))/(2 c L R)))) (L+c R Rr+(Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])) (L-(E^(((Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])  (t-tswitch))/(c L R))) L+c (-1+(E^(((Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])  (t-tswitch))/(c L R)))) R (2 R+Rr)-(1+(E^(((Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])  (t-tswitch))/(c L R)))-2 (E^(((L+c R Rr+(Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)]))  (t-tswitch))/(2 c L R)))) (Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])))/(4 c R (R+Rr) (Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)]))+(1/(2 (R+Rr) (Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])))A (E^(-(((L+c R Rr+(Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)]))  (t-tswitch))/(2 c L R)))) L (-(1/(c R))(E^(((Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])  (t-tswitch))/(c L R))) (Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])+(1/L)(E^(((Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])  (t-tswitch))/(c L R))) (2 R+Rr) (Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])-(Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)]) ((1/(c L R))(E^(((Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])  (t-tswitch))/(c L R))) (Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])-(1/(c L R))(E^(((L+c R Rr+(Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)]))  (t-tswitch))/(2 c L R))) (L+c R Rr+(Sqrt[(L^2)+(c^2) (R^2) (Rr^2)-2 c L R (2 R+Rr)])))),t>tswitch}}]}, {t,0,0.03},PlotRange->{-3500,500}, PlotPoints -> 1000], {R, 0.0000001, 1, Appearance -> "Labeled"}, SaveDefinitions -> True]